﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Fcdbas.Core.Util;
using Fcdbas.Core.Net;
namespace FileTransferProtocol
{
    public sealed class ConstValue
    {
        public static int BlockCount = 4;
        public static int SendSize = 10240;
    }
    public class ThreadSession
    {
        public ConnectionEventArgs Connection;
        public NetSession Session;
    }
    [Serializable]
    public class TransferHead
    {
        /// <summary>
        /// 唯一标识
        /// </summary>
        public string Guid
        {
            get;
            set;
        }
        public TFileInfo FileInfo
        {
            get;
            set;
        }
        public List<FileBlock> Blocks
        {
            get;
            set;
        }
        public static TransferHead CreateTransferHead(string filePath)
        {
            TransferHead head = new TransferHead();

            TFileInfo fi =TFileInfo.GetFileInfo(filePath);
            //fi.FileName = FileHelper.GetFileNameNoPath(filePath);
            //fi.FilePath = FileHelper.GetFileDir(filePath);
            //fi.FileType = System.IO.Path.GetExtension(filePath).Replace('.',' ').Trim();
            //fi.FileSize = FileHelper.GetFileSize(filePath);
            //fi.MD5 = Fcdbas.Core.Security.DataHelper.HashFile(filePath);
            head.FileInfo = fi;
            head.Guid = System.Guid.NewGuid().ToString();
            long size = fi.FileSize / ConstValue.BlockCount;
            head.Blocks = new List<FileBlock>();
            for (int i = 0; i < ConstValue.BlockCount; i++)
            {
                FileBlock block = new FileBlock();
                block.FileName = filePath;
                block.Guid = head.Guid;
                block.HasTransfer = 0;
                block.BlockId = i.ToString();
                block.Complated = false;
                block.BeginPos = i * size;

                if (i == ConstValue.BlockCount - 1)
                {
                    block.EndPos = fi.FileSize - 1;
                    block.TotalSize = fi.FileSize - block.BeginPos;
                }
                else
                {
                    block.EndPos = ((i + 1) * size) - 1;
                    block.TotalSize = size;
                }
                block.TempFile = fi.FileName + i.ToString();
                head.Blocks.Add(block);
            }
            return head;
        }
    }
}
